<?php
class fragmentController extends adminController{
    protected $layout = 'layout';
    //首页
    public function index(){
        $act = in($_GET['act']);
        if($act == 'del'){
            $fid = intval($_GET['id']);
            $condition['id'] = $fid;
            $result =  model('fragment')->delete($condition);
            if($result){
                echo 1;
            }else{
                echo '删除失败!';
            } 
        }else{
            $listRows = 20;
            $url = url('fragment/index',array('page'=>'{page}'));
            $allcount = model('fragment')->count();
            $limit = $this->pageLimit($url,$listRows);
            $this->page= $this->pageShow($allcount);
            $this->list = model('fragment')->select(null,null,null,$limit); 
            $this->display('fragment/index');
        }
    }
    //添加
    public function add(){
        if($this->isPost()){
            $msg = Check::rule(
                    array(check::must($_POST['title']),'碎片名称必须填写'),
                    array(check::must($_POST['about']),'碎片简述必须填写')
                    );
            if(true !== $msg){
                $this->pageMsg($msg);
            }
            //插入模型数据
            $data = array();
            $data = postinput($_POST,array('fid'));
            $data['uptime'] = time();
            $fid = intval($_POST['fid']);
            if($fid){
                $condition['id'] = $fid;
                $info = model('fragment')->update($condition,$data); //插入模型数据  
            }else{
                $info = model('fragment')->insert($data);
            }
            if($info){
                $this->redirect(url('fragment/index'));
            }else{
                $this->pageMsg('资源碎片设置失败');
            }
        }else{
            $this->type = intval($_GET['type']);
            $this->language = $this->model->table('cms_language')->field('id,types,name')->select(); 
            $this->display('fragment/edit');
        } 
    }
    //编辑
    public function edit(){
        $this->fid = intval($_GET['id']);
        $this->type = intval($_GET['type']);
        $this->info = model('fragment')->find('id = '. $this->fid);
        $this->language = $this->model->table('cms_language')->field('id,types,name')->select(); 
        $this->display('fragment/edit'); 
    }
    //################################
    //用户列表
    public function lists(){
        $Cookie = new Cookie();
        $language = $Cookie->get('siteid');
        if($language){
            $condition['language'] = $language;
        }
        //分页开始
        $listRows = 15; //每页条数
        $url = url('fragment/lists',array('page'=>'{page}'));
        $allcount = model('diypage')->count();
        $limit = $this->pageLimit($url,$listRows);
        $this->page= $this->pageShow($allcount);
        //分页结束
        $this->list = model('fragment')->select($condition,null,null,$limit); 
        $this->display('fragment/lists');
    }
    //用户编辑
    public function listsedit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['title']),'自定义名称必须填写'));
            if(true !== $msg){
                $this->pageMsg($msg);
            }
            $data = array();
            $data = postinput($_POST,array('fid'));
            $data['uptime'] = time();
            $fid = intval($_POST['fid']);
            if($fid){
                $condition['id'] = $fid;
                $info = model('fragment')->update($condition,$data);
            }
            if($info){
                $this->redirect(url('fragment/lists'));
            }else{
                $this->pageMsg('自定义设置失败');
            }
        }else{
            $this->fid = intval($_GET['id']);
            $this->info = model('fragment')->find('id = '. $this->fid);
            $this->display('fragment/listsedit');
        }
    }
}